<% if params[:id] && (worker = Resque::Worker.find(params[:id])) && (data = worker.job) %>
  <h1><%= worker %>'s job</h1>

  <table>
    <tr>
      <th>&nbsp;</th>
      <th>Where</th>
      <th>Queue</th>
      <th>Started</th>
      <th>Class</th>
      <th>Args</th>
    </tr>
      <tr>
        <td><img src="<%=u 'working.png' %>" alt="working" title="working"></td>
        <% host, pid, _ = worker.to_s.split(':') %>
        <td><a href="<%=u "/workers/#{worker}" %>"><%= host %>:<%= pid %></a></td>
        <% queue = data['queue'] %>
        <td><a class="queue" href="<%=u "/queues/#{queue}" %>"><%= queue %></a></td>
        <td><span class="time"><%= data['run_at'] %></span></td>
        <% payload = data.key?('payload') ? data['payload'] : {} %>
        <td>
          <code><%= payload.key?('class') ? payload['class'] : "—" %></code>
        </td>
        <td><%=h payload.key?('args') ? payload['args'].inspect : "—" %></td>
      </tr>
  </table>

<% else %>

  <%
    workers = resque.working
    jobs = workers.collect {|w| w.job }
    worker_jobs = workers.zip(jobs)
    worker_jobs = worker_jobs.reject { |w, j| w.idle? }
  %>

  <h1 class='wi'><%= worker_jobs.size %> of <%= resque.workers.size %> Workers Working</h1>
  <p class='intro'>The list below contains all workers which are currently running a job.</p>
  <table class='workers'>
    <tr>
      <th>&nbsp;</th>
      <th>Where</th>
      <th>Queue</th>
      <th>Processing</th>
    </tr>
    <% if worker_jobs.empty? %>
    <tr>
      <td colspan="4" class='no-data'>Nothing is happening right now...</td>
    </tr>
    <% end %>

    <% worker_jobs.sort_by {|w, j| j['run_at'] ? j['run_at'].to_s() : '' }.each do |worker, job| %>
      <tr>
        <td class='icon'><img src="<%=u state = worker.state %>.png" alt="<%= state %>" title="<%= state %>"></td>
        <% host, pid, queues = worker.to_s.split(':') %>
        <td class='where'><a href="<%=u "/workers/#{worker}" %>"><%= host %>:<%= pid %></a></td>
        <td class='queues queue'>
          <a class="queue-tag" href="<%=u "/queues/#{job['queue']}" %>"><%= job['queue'] %></a>
        </td>
        <td class='process'>
          <% if job['queue'] %>
            <code><%= job['payload']['class'] %></code>
            <small><a class="queue time" href="<%=u "/working/#{worker}" %>"><%= job['run_at'] %></a></small>
          <% else %>
            <span class='waiting'>Waiting for a job...</span>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>

<% end %>
